package com.hxf.common;

import java.util.ArrayList;
import java.util.List;

/**
 * 树形
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class Tree {

	public static String ROOT_ID = "0";

	private ArrayList<Node> nodeListTemp = new ArrayList<Node>();
	private ArrayList<Selector> selectorList = new ArrayList<Selector>();
	private Node rootNode = new Node();

	public Node getRootNode() {
		return rootNode;
	}

	public void setRootNode(Node rootNode) {
		this.rootNode = rootNode;
	}

	public Tree() {
		rootNode.setId(ROOT_ID);
		rootNode.setName("www.highesthelp.com");
		nodeListTemp.add(rootNode);
	}

	public Tree(String rootId,String nodeName) {
		rootNode.setId(rootId);
		rootNode.setName(nodeName);
		nodeListTemp.add(rootNode);
	}
	
	
	public void addNode(Node node) {
		String pId = node.getPerantId();
		for (Node n : nodeListTemp) {
			if (pId != null && pId.equals(n.getId())) {
				if (n.getChildNodes() == null) {
					n.setChildNodes(new ArrayList<Node>());
				}
				n.getChildNodes().add(node);
			}
		}
		nodeListTemp.add(node);
	}

	public String toXMLString() {
		StringBuffer retValue = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		retValue.append("<tree id=\"" + rootNode.getId() + "\" text=\""
				+ rootNode.getName() + "\">");
		retValue.append(viewNode(rootNode, true));
		retValue.append("</tree>");
		System.out.println(retValue.toString());
		return retValue.toString();
	}

	private String viewNode(Node rootNode, boolean isOpen) {
		StringBuffer retValue = new StringBuffer("");
		if (rootNode.getChildNodes() != null
				&& rootNode.getChildNodes().size() > 0) {
			for (Node node : rootNode.getChildNodes()) {
				retValue.append("<item id=\""
						+ node.getId()
						+ "\" text=\""
						+ node.getName()
						+ "\" "
						+ (isOpen == true ? "open=\"1\"" : "")
						+ " "
						+ ((node.getIco() != null) ? "im0=\"" + node.getIco()
								+ "\"" : "") + ">");
				if (node.getChildNodes() != null
						&& node.getChildNodes().size() > 0) {
					retValue.append(viewNode(node, true));
				}
				if(PublicUtil.isNotNull(node.getUrl())){
					retValue.append("<userdata name=\"url\">" + node.getUrl() + "</userdata>");
				}
				retValue.append("</item>");
			}
		}
		return retValue.toString();
	}

	public List viewSelectNode(Node rootNode, int pos) {
		if (rootNode.getChildNodes() != null
				&& rootNode.getChildNodes().size() > 0) {
			int p = pos + 1;
			for (Node node : rootNode.getChildNodes()) {
				selectorList.add(new Selector(Integer.valueOf(node.getId()),"|" + calcSpace(pos) + node.getName()));
				if (node.getChildNodes() != null
						&& node.getChildNodes().size() > 0) {
					viewSelectNode(node, p);
				}
			}
		}
		return selectorList;
	}

	public List<Selector> viewSelectNodeIncludeMe(Node rootNode, int pos) {
		selectorList.add(new Selector(Integer.valueOf(rootNode.getId()),rootNode.getName()));
		viewSelectNode(rootNode,1);
		//System.out.println(this.toXMLString());
		return  selectorList;
	}
	
	
	
	private String calcSpace(int pos) {
		String sbnp = "——";
		String retValue = "";
		for (int i = 0; i < pos; i++) {
			retValue += sbnp;
		}
		if (pos == 0) {
			return retValue;
		}
		return  retValue;
	}

	public List<Selector> toSelected() {
		return viewSelectNode(rootNode, 0);
	}

	/***************************************************************************
	 * 获得子部门Pk
	 * 
	 * @param rootNode
	 * @return
	 */
	private String getSubPks(Node rootNode) {
		//Node rootNode = this.rootNode;
		StringBuffer retValue = new StringBuffer("");
		if (rootNode.getChildNodes() != null
				&& rootNode.getChildNodes().size() > 0) {
			//retValue.append(rootNode.getName() + ",");
			for (Node node : rootNode.getChildNodes()) {
				retValue.append(node.getId() + ",");
				if (node.getChildNodes() != null
						&& node.getChildNodes().size() > 0) {
					retValue.append(getSubPks(node));
				}
			}
		}
/*		String rv = retValue.toString();
		if(rv.endsWith(",")){
			return rv.substring(0, rv.length() - 1);
		}*/
		if(retValue.toString().endsWith(",")){
			return retValue.substring(0, retValue.length() - 1);
		}
		return null;
	}
	

	
	

	public String treeSubPks(){
		String retValue = getSubPks(rootNode);
		if(retValue.endsWith(",")){
			return retValue.substring(0, retValue.length() - 1);
		}
		return "";
	}
	
	
	
	public static void main(String[] args) {

/*		Tree tree = new Tree();

		Node n7 = new Node("7", "网易", ROOT_ID);
		Node n8 = new Node("8", "movie", "7");

		Node n1 = new Node("11", "中国", "8");
		Node n2 = new Node("12", "美国", "8");
		Node n3 = new Node("13", "欧洲", "8");
		Node n4 = new Node("14", "日本", "8");

		Node n9 = new Node("9", "new", "7");
		Node n10 = new Node("10", "joke", "7");

		Node n15 = new Node("15", "新浪", ROOT_ID);
		Node n16 = new Node("16", "生活", "15");
		Node n17 = new Node("17", "财经", "15");

		tree.addNode(n7);
		tree.addNode(n8);
		tree.addNode(n9);
		tree.addNode(n10);
		tree.addNode(n1);
		tree.addNode(n2);
		tree.addNode(n3);
		tree.addNode(n4);
		tree.addNode(n15);
		tree.addNode(n16);
		tree.addNode(n17);
		
		*/
		
		
		Tree tree = new Tree();
		
		
		Node n1 = new Node("1", "超级管理员部门", "0");
		Node n2 = new Node("2", "新部门", "1");
		Node n3 = new Node("3", "新部门下面的部门", "2");
		Node n4 = new Node("4", "技术部", "1");
		
		Node n5 = new Node("5", "技术部1", "1"); 
		List<Node> nodeList = new ArrayList<Node>();
		nodeList.add(n5);
		n4.setChildNodes(nodeList);
		Node meNode = n1;

		tree.addNode(n1);
		tree.addNode(n2);
		tree.addNode(n3);
		tree.addNode(n4);
		
		List<Selector> sor2 = tree.viewSelectNodeIncludeMe(meNode, 0);
		
	
		
		for(Selector s : sor2){
			System.out.println(s.getName());
		}
		
		
		
		System.out.println(tree.toXMLString());

	}
}
